import {
  get,
  postParam
} from "../../utils/service"
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: null,
    printable: false,
    printFee: 0,
    needPrint: !!app.globalData.recevier,
    paperFee: app.globalData.paperFee,
    totalFee: 0,
    recevier: app.globalData.recevier,
    submitLoading: false,
    paying: false
  },
  getOrderInfo: function () {
    const self = this
    //const orderId = app.globalData.orderId
    const orderId = this.options.orderId
    get(`${app.globalData.host}/order/view/${orderId}`, {}, {
      success: function (data) {
        console.info("================== order info ===============", data)
        const size = app.globalData.recevier ? data.items.length : 0
        const printable = !data.combo.includeBoke
        const printFee = size * app.globalData.paperFee
        self.setData({
          order: data,
          printFee,
          printable,
          totalFee: printFee + data.fee,
        })
      }
    })
  },
  onNeedPrintChange: function ({
    detail
  }) {
    const size = detail ? this.data.order.items.length : 0
    const printFee = size * app.globalData.paperFee
    if (!detail) {
      app.globalData.recevier = null
    }
    this.setData({
      needPrint: detail,
      printFee,
      totalFee: printFee + this.data.order.fee,
      recevier: app.globalData.recevier
    })
  },
  toChooseRecevier: function () {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrderInfo()
    this.setData({
      recevier: app.globalData.recevier,
      needPrint: !!app.globalData.recevier,
    })
  },

  onSubmit: function () {
    this.setData({
      submitLoading: true,
      paying: true
    })
    const self = this
    const orderId = this.data.order.id
    const param = {}
    if (this.data.needPrint) {
      if (!app.globalData.recevier) {
        //todo提示
      } else {
        param.receiverId = app.globalData.recevier.id
        param.printSizes = this.data.combo.includeSize.split(',')
      }
    }
    postParam(`${app.globalData.host}/order/submit/${orderId}`, param, {
      success: function (data) {
        if (data && data.orderNo) {
          if (data.status === 'submitted') {
            self.toPay(data.id)
          } else {
            Dialog.alert({
              title: '提示',
              message: 'VIP订单已成功,请在电脑端下载订单文件',
            }).then(() => {
              wx.navigateTo({
                url: '"/pages/myorder/myorder"',
              })
            })
          }
        }
      },
      complete: function () {
        self.setData({
          submitLoading: false
        })
      }
    })
  },
  onPay: function () {
    const orderId = this.data.order.id
    this.toPay(orderId)
  },
  toPay: function (orderId) {
    Toast.loading({
      message: '正在生成支付信息...',
      forbidClick: true,
    })
    const self = this
    const url = `${app.globalData.host}/order/mini/toPay/${orderId}/${app.globalData.openId}`
    postParam(url, {
      payType: 'wxMini'
    }, {
      success: function (data) {
        const param = {
          ...data,
          package: data.packageContent,
          complete: function () {
            Dialog.confirm({
              title: '提示',
              confirmButtonText: '支付已完成',
              cancelButtonText: '重新支付',
              message: '支付结果',
            }).then(() => {
              wx.navigateTo({
                url: `/pages/order-result/order-result?orderId=${orderId}`,
              })
            }).catch(() => {
              self.toPay(orderId)
            })
          }
        }
        delete param.packageContent
        wx.requestPayment(param)
      },
      fial: function (err) {
        console.err(err)
      },
      complete: function () {
        Toast.clear()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.needPrint && !app.globalData.recevier) {

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})